package ocg.common.re.influx.GatlingParser;

import net.quux00.simplecsv.CsvParser;
import net.quux00.simplecsv.CsvParserBuilder;
import net.quux00.simplecsv.CsvReader;
import net.quux00.simplecsv.CsvReaderBuilder;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenbo on 2018/2/9.
 */
public class GatlingCsvParser {

    private final File file;

    public GatlingCsvParser( File file ) {
        this.file = file;
    }

    /**
     * 处理 simulation.log 的 csv 数据 ， 返回 SimulationContext 类
     * @return
     * @throws IOException
     */
    public SimulationContext csvParser() throws IOException {

        SimulationContext context = new SimulationContext ();

        CsvParser csvParser = new CsvParserBuilder()
                                .trimWhitespace ( true )        //去掉前后空白字符
                                .allowUnbalancedQuotes ( true )         //
                                .separator ( '\t' )         // 分割
                                .build ();

        CsvReader reader = new CsvReaderBuilder ( new FileReader ( this.file ) ).csvParser ( csvParser ).build ();

        List<String> header = reader.readNext();

        context.setSimulationName ( header.get ( 2 ) );
        context.setSimulationStartTime ( Long.parseLong( header.get ( 4 ) ) );

        List<String> line ;

        List <HttpRequest> requests = new ArrayList <HttpRequest> (  );
        List <HttpUser> users = new ArrayList <HttpUser> (  );

        while ( null != (line = reader.readNext ())){

            String type = line.get ( 0 );

            if ( type.equals ( "RUN" )){

            }
            else if ( type.equals ( "REQUEST" ) ){
                requests.add ( requestParser ( line ) );
            }
            else if ( type.equals ( "USER" ) ){
                users.add ( userParser( line ) );
            }
        }

        context.setRequests ( requests );
        context.setUsers ( users );

        return context;
    }

    /**
     * 对 request 数据处理，返回 HttpRequest 类
     * @param link
     * @return
     */
    public HttpRequest requestParser(List <String> link ){
        HttpRequest request = new HttpRequest ();
        int size = link.size ();
        request.setScenario ( link.get ( 1 ) );
        request.setUser_ID ( Integer.parseInt ( link.get ( 2 ) ) );
        request.setHttp ( link.get ( 4 ) );

        long StartTime = Long.parseLong ( link.get ( 5 ) );
        long EndTime = Long.parseLong ( link.get ( 6 ) );

        request.setStartTime ( StartTime );
        request.setEndTime ( EndTime );

        long reTime = EndTime - StartTime ;

        request.setSuccess ( link.get ( 7 ) );

        if ( size == 8 ){
            request.setError ( null );
        }
        if ( size == 9 ){
            request.setError ( link.get ( 8 ) );
        }

        request.setResponseTime ( reTime );

        return request;
    }

    /**
     * 对 user 数据处理，返回 HttpUser 类
     * 区分 START 和 END ， TIME 对应 START TIME OR END TIME
     * @param link
     * @return
     */
    public HttpUser userParser(List<String> link ){
        HttpUser user = new HttpUser ();

        user.setScenario ( link.get ( 1 ) );
        user.setUser_ID ( Integer.parseInt ( link.get ( 2 ) ) );
        String UserType = link.get ( 3 );
        user.setUserType ( UserType );
        if ( UserType.equals ( "START" )){
            user.setTime ( Long.parseLong ( link.get ( 4 ) ) );
        }
        if ( UserType.equals ( "END" )){
            user.setTime ( Long.parseLong ( link.get ( 5 ) ) );
        }
        return user;
    }

}
